# -*- coding: utf-8 -*-
# Created by methew at 2018/10/28
"""
File:               LinuxBashShellScriptForOps:pyLinuxHostsSSHKeyInitialization.py
User:               jinke
Create Date:        2018/2/2
Create Time:        11:24
 """
import time
import os
import logging
import logging.handlers
import sys
from fabric.api import *
from fabric.colors import red, green, yellow, blue, magenta, cyan, white
from fabric.context_managers import *
from fabric.contrib.console import confirm
import server as serverfile

env.roledefs = serverfile.roledefs

env.passwords = serverfile.passwords

env.port = '22'
env.user = "root"
env.sudo_user = "root"  # fixed setting, it must be 'root'
env.disable_known_hosts = True
env.warn_only = False
# env.command_timeout = 25
env.connection_attempts = 2
# 判断
mswindows = (sys.platform == "win32") # learning from 'subprocess' module
linux = (sys.platform == "linux2")


def initLoggerWithRotate(logPath="/var/log", logName=None, singleLogFile=True):
    current_time = time.strftime("%Y%m%d%H")
    if logName is not None and not singleLogFile:
        logPath = os.path.join(logPath, logName)
        logFilename = logName + "_" + current_time + ".log"
    elif logName is not None and singleLogFile:
        logPath = os.path.join(logPath, logName)
        logFilename = logName + ".log"
    else:
        logName = "default"
        logFilename = logName + ".log"
    if not os.path.exists(logPath):
        os.makedirs(logPath)
        logFilename = os.path.join(logPath, logFilename)
    else:
        logFilename = os.path.join(logPath, logFilename)
    logger = logging.getLogger(logName)
    log_formatter = logging.Formatter("%(asctime)s %(filename)s:%(lineno)d %(name)s %(levelname)s: %(message)s", "%Y-%m-%d %H:%M:%S")
    file_handler = logging.handlers.RotatingFileHandler(logFilename, maxBytes=104857600, backupCount=5)
    file_handler.setFormatter(log_formatter)
    stream_handler = logging.StreamHandler(sys.stderr)
    logger.addHandler(file_handler)
    logger.addHandler(stream_handler)
    logger.setLevel(logging.DEBUG)
    return logger
def is_windows():
    if 'posix' in sys.builtin_module_names:
        os_type='Linux'
    else:
        os_type='Windows'
        
if __name__ == '__main__':
    import re
    if len(sys.argv)==1:
        if is_wind